package com.clstu.syn;

public class DeadLock_ {
    public static void main(String[] args) {
       DeadLockDemo A = new DeadLockDemo(true);
       DeadLockDemo B = new DeadLockDemo(false);
       A.setName("A线程");
       B.setName("B线程");
       A.start();
       B.start();//产生死锁，互相强对方的对象锁
    }
}
class DeadLockDemo extends Thread{
    public static Object o1 = new Object();//静态公用对象（只有两个），谁锁到就是谁的，没有多余的
    public static Object o2 = new Object();
    private boolean flag;

    public DeadLockDemo(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if(flag){
            synchronized(o1){//对象互斥锁，下面就是同步代码
                System.out.println(Thread.currentThread().getName()+"进入o1");
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"进入o2");
                }
            }
        }else{
            synchronized(o2) {
                System.out.println(Thread.currentThread().getName() + "进入o3");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + "进入o4");
                }
            }
        }
    }
}